#!/bin/bash

# 输入参数：commit ID
commitid=$1

# 获取提交时间
commit_time=$(git show -s --format=%ci $commitid)

echo "Commit time for $commitid is $commit_time"

# 使用 repo forall 命令切换其他仓库
repo forall -c '
  echo "Processing $REPO_PATH";
  cd $REPO_PATH;
  # 使用提交时间找到最接近的提交
  nearest_commit=$(git rev-list -n 1 --before="'$commit_time'" master);
  if [ -n "$nearest_commit" ]; then
    git checkout $nearest_commit;
    echo "Checked out to $nearest_commit in $REPO_PATH";
  else
    echo "No commit found before '$commit_time' in $REPO_PATH";
  fi
'
